<?php

namespace App\Admin\Controllers;

use App\Models\Order;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Table;
class OrderController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单');
            $content->description('订单列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        Admin::script($this->script());
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->disableCreation();
            $grid->actions(function ($actions) {
                // prepend一个操作
                $actions->prepend('<a href="'.url('admin/orderGoods?order_id='.$actions->getKey()).'"><i class="fa fa-paper-plane"></i></a>');
            });
            $grid->id('ID')->sortable();
            $grid->user_id('用户id');
            $grid->mobile('联系电话');
            $grid->university('大学');
            $grid->order_amount('应付款');
            $grid->use_balance_amount('使用余额');
            $grid->use_balance_status('余额支付状态')->display(function ($data){
                if($this->use_balance_amount==0){
                    return '不需要支付';
                }
                return $data?'已支付':'未支付';
            });
            $grid->pay_amount('现金支付');
            $grid->pay_status('现金支付状态')->display(function ($data){
                if($this->pay_amount==0){
                    return '不需要支付';
                }
                return $data?'已支付':'未支付';
            });
            $grid->cancel('取消订单')->display(function ($data){
               return $data?'是':'否';
            });
            $grid->order_status('订单状态')->display(function ($data){
                switch ($data){
                    case 0:return '待付款';
                    case 1:return '已付款(待取书)';
                    case 2:return '已取书';
                    case 3:return '退款（包含部分退款和全部退款）';
                    case 4:return '取消订单';
                    case 5:return '订单过期';
                    default:return '异常';
                }
            });
            $grid->goods_num('商品数量');
            $grid->more('更多')->display(function(){

                $rows = [
                    ['订单编号', $this->order_sn],
                    ['校区', $this->campus],
                    ['下单时间',date('Y-m-d H:i:s',$this->add_time)],
                    ['支付时间',date('Y-m-d H:i:s',$this->pay_time)],

                ];

                $table = new Table([], $rows);

                return <<<DATA
<a class="btn btn-xs btn-default grid-expand" data-inserted="0" data-key="{$this->id}" 
data-toggle="collapse" data-target="#grid-collapse-{$this->id}">
    <i class="fa fa-caret-right"></i> 详情
</a>
<template class="grid-expand-{$this->id}">
    <div id="grid-collapse-{$this->id}" class="collapse">
   {$table}
</div>
</template>
DATA;

            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {

        });
    }

    protected function script()
    {
        return <<<SCRIPT
$('.grid-expand').on('click', function () {
     if ($(this).data('inserted') == '0') {
        var key = $(this).data('key');
        var row = $(this).closest('tr');
        var html = $('template.grid-expand-'+key).html();

        row.after("<tr><td colspan='"+row.find('td').length+"' style='padding:0 !important; border:0px;'>"+html+"</td></tr>");

        $(this).data('inserted', 1);
    }

    $("i", this).toggleClass("fa-caret-right fa-caret-down");

});

SCRIPT;
    }
}
